package com.itbour.web.action.yjx.tmpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;



import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.yjx.tmpl.YjxTmplSetInfoDto;
import com.itbour.service.yjx.tmpl.YjxTmplGrpService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 	@author 李琦
 * 	模板组管理action
 */
public class YjxTmplGrpAction extends BaseAction{

	/**
	 */
	private static final long serialVersionUID = 1L;
	private YjxTmplGrpService yjxTmplGrpService;// 模板组
	private String findUse;//用途
	private String findPubFlg;//是否发布
	private String findTmplSTitle;//标题
	private String findTmplSDesc;// 用途介绍
	private String findTmplTsId;//模板组ID
	private String findDtS;//起始时间
	private String findDtE;//截止时间
	
	
	
	//   分页获取模板组列表
	private Map<String, Object> tmplSetMap =new HashMap<String, Object>();
	public String getTmplSetInfoPg() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		Map<String, Object> param = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(findTmplSTitle)) {
			param.put("title", "%"+findTmplSTitle+"%");
		}
		if (StringUtils.isNotEmpty(findTmplTsId)) {
			param.put("set_id", findTmplTsId);
		}
		if (StringUtils.isNotEmpty(findDtS)) {
			param.put("start_time", findDtS);
		}
		if (StringUtils.isNotEmpty(findDtE)) {
			param.put("end_time", findDtE);
		}
		if (StringUtils.isNotEmpty(findUse)) {
			param.put("use", findUse);
		}
		if (StringUtils.isNotBlank(findPubFlg)) {
			param.put("publish", findPubFlg);
		}
		if (StringUtils.isNotBlank(findTmplSDesc)) {
			param.put("desc", findTmplSDesc);
		}
		Page page = yjxTmplGrpService.pageTmplSetInfo(param,
				Integer.parseInt(pageNumber),
				Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		tmplSetMap = resMap;
		return SUCCESS;
	}
	public String getFindTmplSDesc() {
		return findTmplSDesc;
	}
	public void setFindTmplSDesc(String findTmplSDesc) {
		this.findTmplSDesc = findTmplSDesc;
	}
	
	



	// 添加模板组
	private YjxTmplSetInfoDto tmplSetDto;
	public void addTmplSet() throws Exception {
		if (tmplSetDto != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if (user != null) {
	    		tmplSetDto.setUpdate_by(user.getUser_id().toString());
	    	}
	    	if(tmplSetDto.getSet_id()!=null && tmplSetDto.getSet_id()>0){
	    		// 修改模板组信息
	    		this.updTmplSet();
	    	} else {
	    		// 新建
	    		yjxTmplGrpService.addTmplSet(tmplSetDto);
	    		this.writeJSON("ok");
	    	}
		}
	}
	
	// 修改模板组
	public void updTmplSet() throws Exception {
		try {
			yjxTmplGrpService.upTmplSet(tmplSetDto);
			this.writeJSON("ok");
		} catch (Exception e) {
			this.writeJSON("no");
		}
	}
	
	// 获取单个模板组信息
	private List<YjxTmplSetInfoDto> tmplLis;
	public String getTmplSetLst() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("set_id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("set_id", id);
		}
		tmplLis = yjxTmplGrpService.getOneTmplInfo(param);
		return SUCCESS;
	}
	
	// 发布模板组
	public void publishTmplSet() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		if(user!=null && user.getUser_id()!=null){
			if (StringUtils.isNotBlank(ids)) {
	    		List<Long> listId = StringUtils.stringToList2(ids);
	    		Map<String, Object> parm =new HashMap<String, Object>();
	    		parm.put("publish", 1);
	    		parm.put("ids", StringUtils.listToString2(listId));
	    		parm.put("update_by", user.getUser_id());
	    		yjxTmplGrpService.upTmplSet(parm);
	    		
	    	}
		}
		this.writeJSON("ok");
	}
	
	public void cancelPublishTmplSet() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		if(user!=null && user.getUser_id()!=null){
			if (StringUtils.isNotBlank(ids)) {
	    		List<Long> listId = StringUtils.stringToList2(ids);
	    		Map<String, Object> parm =new HashMap<String, Object>();
	    		parm.put("publish", 0);
	    		parm.put("ids", StringUtils.listToString2(listId));
	    		parm.put("update_by", user.getUser_id());
	    		yjxTmplGrpService.upTmplSet(parm);
	    	}
		}
		this.writeJSON("ok");
	}
	
	public void upTmpSetOrder() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if(user!=null && user.getUser_id()!=null){
    		HttpServletRequest request = ServletActionContext.getRequest();
    		String setId = request.getParameter("setId");
    		String order = request.getParameter("order");
	    	Map<String, Object> param = new HashMap<String, Object>();
			param.put("order", Integer.valueOf(order));
			param.put("set_id", Long.valueOf(setId));
			param.put("update_by", user.getUser_id());
			yjxTmplGrpService.upTmplSet(param);
			this.writeJSON("ok");
    	}		
	}
	
	// 修改媒体的排序
	public void upMediaOrder() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	HttpServletRequest request = ServletActionContext.getRequest();
    	String media_id = request.getParameter("media_id");// 
    	String order = request.getParameter("order");
    	if (user!=null && user.getUser_id()!=null && media_id!=null) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("order", Integer.valueOf(order));
			param.put("media_id", Long.valueOf(media_id));
			param.put("update_by", user.getUser_id());
			yjxTmplGrpService.updMediaOrder(param);
			this.writeJSON("ok");
    	} else {
    		this.writeJSON("no");
    	}	
	}
	
	// 加载模板组下的设计模板
	private Map<String, Object> tmplR =new HashMap<String, Object>();
	private String findTgId;// 模板ID
	private String findTgTitle;// 模板标题
	public String getTmplSetRInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Integer set_id = Integer.parseInt(request.getParameter("setId"));
			if(set_id != null){
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("set_id",set_id);
				if (StringUtils.isNotEmpty(findTgId)){
					param.put("design_id",findTgId);
				}
				if (StringUtils.isNotEmpty(findTgTitle)){
					param.put("title",findTgTitle);
				}
				Page page = yjxTmplGrpService.getTmplGroupDesignInfo(param,
							Integer.parseInt(pageNumber),
							Integer.parseInt(pageSize));
				HashMap<String, Object> resMap = new HashMap<String, Object>();
				resMap.put("total", page.getTotalCount());
				resMap.put("rows", page.getResult());
				tmplR = resMap;
			}
		}
		return SUCCESS;
	}
	
	
	private String findVip;
	private String findTmplName;
	private String findTmplId;
	//  获取适合的视频设计模板列表
	private Map<String, Object> suitViDesi = new HashMap<String, Object>();
	public String getViSuitDesignInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Integer tmplType = Integer.parseInt(request.getParameter("tmplType"));
			String setId = request.getParameter("setId");
			if (tmplType != null && tmplType > 0 && setId !=null){
				Map<String, Object> param = new HashMap<String, Object>();
				if (StringUtils.isNotBlank(findVip)) {
	    			param.put("vip", findVip);
	    		}
				if (StringUtils.isNotEmpty(findTmplName)) {
	    			param.put("design_name", "%"+findTmplName+"%");
	    		}
				if (StringUtils.isNotBlank(findTmplId)) {
	    			param.put("tmpl_id", findTmplId);
	    		}
				param.put("tmpl_type",tmplType);
				param.put("set_id",setId);
				Page page = yjxTmplGrpService.getSuitViDesignInfo(param,
							Integer.parseInt(pageNumber),
							Integer.parseInt(pageSize));
				HashMap<String, Object> resMap = new HashMap<String, Object>();
				resMap.put("total", page.getTotalCount());
				resMap.put("rows", page.getResult());
				suitViDesi = resMap;
			}
		}
		return SUCCESS;
	}
	public Map<String, Object> getSuitViDesi() {
		return suitViDesi;
	}
	public void setSuitViDesi(Map<String, Object> suitViDesi) {
		this.suitViDesi = suitViDesi;
	}
	
	

	/*		获取适合的设计模板
	 */
	private String findTmplType; //  查找类型
	private Map<String, Object> suitDesi =new HashMap<String, Object>();
	public String getSuitDesignInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			String setId = request.getParameter("setId");
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotBlank(findVip)) {
    			param.put("vip", findVip);
    		}
			if (StringUtils.isNotEmpty(findTmplName)) {
    			param.put("design_name", "%"+findTmplName+"%");
    		}
			if (StringUtils.isNotBlank(findTmplId)) {
    			param.put("design_id", findTmplId);
    		}
			if (StringUtils.isNotBlank(findTmplType)) {
				param.put("type_id", findTmplType);
			}
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			if (setId != null){
				param.put("set_id",setId);
				Page page = yjxTmplGrpService.getSuitDesignInfo(param,
						Integer.parseInt(pageNumber),
						Integer.parseInt(pageSize));
				resMap.put("total", page.getTotalCount());
				resMap.put("rows", page.getResult());
			}
			suitDesi = resMap;	
		}
		return SUCCESS;
	}
	public String getFindTmplType() {
		return findTmplType;
	}
	public void setFindTmplType(String findTmplType) {
		this.findTmplType = findTmplType;
	}
	
	
	
	

	// 给模板组添加合适的模板
	public void addSuitDesign() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer setId = Integer.parseInt(request.getParameter("setId"));
			String uTmplId = request.getParameter("uTmplId");
			Integer type = Integer.parseInt(request.getParameter("type"));
			if(setId !=null && uTmplId != null){
				List<Long> list = StringUtils.stringToList2(uTmplId);
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("set_id",setId);
				param.put("update_by",user.getUser_id());
				
				boolean  b = false;//定义判断是否完成
				
				if (type == 2) {// 添加媒体到分组
					for (int i = 0; i < list.size(); i++) {
						param.put("uni_tmpl_id", list.get(i));
						b=yjxTmplGrpService.addSuitMedia(param);
					}
				} else {
					for (int i = 0; i < list.size(); i++) {
						param.put("uni_tmpl_id", list.get(i));
						b=yjxTmplGrpService.addSuitDesign(param);
					}
				}
					if(b){
						this.writeJSON("ok");
					}else{
						this.writeJSON("no");
					}
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	// 删除模板组下的 设计模板
	public void delTmplSRDesign() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String rId = request.getParameter("rId");
			if(rId != null){
				Map<String, Object> param = new HashMap<String, Object>();
				List<Long> list = StringUtils.stringToList2(rId);
				param.put("ids",StringUtils.listToString2(list));
				param.put("update_by",user.getUser_id());
				boolean b=yjxTmplGrpService.delTmplSRDesign(param);
				if(b){
					this.writeJSON("ok");
				}else{
					this.writeJSON("no");
				}
			}
		}
	}
	
	// 删除一键秀模板组
	public void delTmplGp() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	HttpServletRequest request = ServletActionContext.getRequest();
    	String ids = request.getParameter("ids");
    	if(user!=null && user.getUser_id()>0 && ids!=null) {
    		Map<String, Object> parm =new HashMap<String, Object>();
    		String setId = StringUtils.listToString2(StringUtils.stringToList2(ids));
    		if (Integer.parseInt(setId)>0){
    			parm.put("set_id",setId);
    			boolean b = yjxTmplGrpService.delTmplGp(parm);
    			if (b){
    				this.writeJSON("ok");
    			} else{
    				this.writeJSON("no");
    			}
    		}
    	} else{
    		this.writeJSON("no");
    	}
	}
	
	/**	查看 适合的媒体文件列表
	 */
	private String findMedaName;
	public String getSuitMediaInfo() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			String setId = request.getParameter("setId");
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotBlank(findTmplId)) {
    			param.put("item_id", findTmplId);
    		}
			if (StringUtils.isNotBlank(findMedaName)) {
				param.put("media_name", findMedaName);
			}
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			if (setId != null){
				param.put("set_id",setId);
				Page page = yjxTmplGrpService.getSuitMediaInfo(param,
						Integer.parseInt(pageNumber),
						Integer.parseInt(pageSize));
				resMap.put("total", page.getTotalCount());
				resMap.put("rows", page.getResult());
			}
			suitDesi = resMap;	
		}
		return SUCCESS;
	}
	
	// ***************************************************以下是生成 set和get方法
	
	public String getFindUse() {
		return findUse;
	}
	public String getFindMedaName() {
		return findMedaName;
	}
	public void setFindMedaName(String findMedaName) {
		this.findMedaName = findMedaName;
	}
	public String getFindVip() {
		return findVip;
	}

	public void setFindVip(String findVip) {
		this.findVip = findVip;
	}

	public String getFindTmplName() {
		return findTmplName;
	}

	public void setFindTmplName(String findTmplName) {
		this.findTmplName = findTmplName;
	}

	public String getFindTmplId() {
		return findTmplId;
	}

	public void setFindTmplId(String findTmplId) {
		this.findTmplId = findTmplId;
	}

	public Map<String, Object> getSuitDesi() {
		return suitDesi;
	}

	public void setSuitDesi(Map<String, Object> suitDesi) {
		this.suitDesi = suitDesi;
	}

	public Map<String, Object> getTmplSetMap() {
		return tmplSetMap;
	}

	public void setTmplSetMap(Map<String, Object> tmplSetMap) {
		this.tmplSetMap = tmplSetMap;
	}
	public void setFindUse(String findUse) {
		this.findUse = findUse;
	}
	public String getFindPubFlg() {
		return findPubFlg;
	}

	public void setFindPubFlg(String findPubFlg) {
		this.findPubFlg = findPubFlg;
	}
	public String getFindTmplSTitle() {
		return findTmplSTitle;
	}
	public void setFindTmplSTitle(String findTmplSTitle) {
		this.findTmplSTitle = findTmplSTitle;
	}
	public String getFindTmplTsId() {
		return findTmplTsId;
	}
	public void setFindTmplTsId(String findTmplTsId) {
		this.findTmplTsId = findTmplTsId;
	}
	public String getFindDtS() {
		return findDtS;
	}
	public void setFindDtS(String findDtS) {
		this.findDtS = findDtS;
	}
	public String getFindDtE() {
		return findDtE;
	}
	public void setFindDtE(String findDtE) {
		this.findDtE = findDtE;
	}
	public YjxTmplGrpService getYjxTmplGrpService() {
		return yjxTmplGrpService;
	}
	public void setYjxTmplGrpService(YjxTmplGrpService yjxTmplGrpService) {
		this.yjxTmplGrpService = yjxTmplGrpService;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public YjxTmplSetInfoDto getTmplSetDto() {
		return tmplSetDto;
	}
	public void setTmplSetDto(YjxTmplSetInfoDto tmplSetDto) {
		this.tmplSetDto = tmplSetDto;
	}

	public List<YjxTmplSetInfoDto> getTmplLis() {
		return tmplLis;
	}
	public void setTmplLis(List<YjxTmplSetInfoDto> tmplLis) {
		this.tmplLis = tmplLis;
	}
	public Map<String, Object> getTmplR() {
		return tmplR;
	}
	public void setTmplR(Map<String, Object> tmplR) {
		this.tmplR = tmplR;
	}
	public String getFindTgId() {
		return findTgId;
	}
	public void setFindTgId(String findTgId) {
		this.findTgId = findTgId;
	}
	public String getFindTgTitle() {
		return findTgTitle;
	}
	public void setFindTgTitle(String findTgTitle) {
		this.findTgTitle = findTgTitle;
	}
}
